<?php
class Controller_Product_Warehouse extends Controller_Abstract
{
	function actionList()
	{
		$this->_view['title'] = '列表';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 20;
		$product_categories = Product_Categories::getCategoriesTree();
		$this->_view['product_categories'] = json_encode($product_categories);
		$user = Purchase_Order::getUser();
		$this->_view['user'] = $user['data'];
		$build_condition = Helper_BSS_Normal::buildCondition();
		$build_condition['limit'] = $user['ack'];
		if (! isset($_GET['is_search']))
		{
			$build_condition['warehouse_id'] = Warehouse::getDefaultWarehouseId();
			$_GET['warehouse_id'] = Warehouse::getDefaultWarehouseId();
		}
		$this->_view['url_arr'] = json_encode($build_condition);
		$search_result = Product_Warehouse::search($build_condition, $cp, $ps);
		if ($search_result['ack'] == SUCCESS)
		{
			$page = new Helper_BSS_Page($search_result['pagination'], url('Product::Warehouse/List', $build_condition));
			$this->_view['page'] = $page->getPage();
			$productinfo = $search_result['data'];
			Helper_BSS_Normal::getProductImgUrl($productinfo);
			$this->_view['product'] = $productinfo;
			$warehouse = Warehouse::find()->asArray()->getAll();
			$this->_view['warehouse'] = $warehouse;
		}
	}
	
	function actionLockFlagSave()
	{
		$this->_view['title'] = '警告库存锁定更新（保存）';
		$product_warehouse_id = Product_Warehouse::find('id=?', $_POST['id'])->asArray()->getOne();
		$lock_warning_quantity_days = isset($_POST['lock_warning_quantity_days']) ? $_POST['lock_warning_quantity_days'] : 0;
		$lock_safe_quantity_days = isset($_POST['lock_safe_quantity_days']) ? $_POST['lock_safe_quantity_days'] : 0;
		$lock_flag = (isset($_POST['lock_flag']) == 'on') ? 1 : 0;
		if ($product_warehouse_id)
		{
			$warehouse_id = Warehouse::getDefaultWarehouseId();
			QDB::getConn()->execute('update product_warehouse set lock_warning_quantity_days=' . $lock_warning_quantity_days . ',lock_safe_quantity_days=' . $lock_safe_quantity_days . ',lock_flag=' . $lock_flag . ' where id=' . $_POST['id'] . ' and warehouse_id=' . $warehouse_id);
			return $this->_redirectMessage('保存成功', '正在返回..', $_SERVER['HTTP_REFERER'], SUCCESS);
		}
	}
	
	function actionEdit()
	{
		$this->_view['title'] = '编辑';
		$product_id = isset($_GET['id']) ? $_GET['id'] : 0;
		$product_categories = Product_Categories::getCategoriesTree();
		$this->_view['product_categories'] = json_encode($product_categories);
		if ($product_id)
		{
			$product = Product_Basic::find('id = ?', $product_id)->asArray()->getAll();
			Helper_BSS_Normal::getProductImgUrl($product);
			$product = $product[0];
			$preferred_provider = Product_Provider::find('id=?', $product['preferred_provider_id'])->asArray()->getOne();
			$this->_view['preferred_provider'] = $preferred_provider;
			$this->_view['product'] = $product;
		}
		else
		{
			return $this->_redirect(url('Product::Warehouse/List'));
		}
	}
	
	function actionEditSave()
	{
		$this->_view['title'] = '编辑（保存）';
		if ($this->_context->isPOST())
		{
			$result = Product_Basic::edit($_POST);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('编辑成功！', '正在返回..', url('Product::Warehouse/List'), $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('编辑失败！', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
		else
		{
			return $this->_redirectMessage('编辑失败！', '不是提交方式有误！', $_SERVER['HTTP_REFERER'], FAILURE);
		}
	}
	
	function actionProviderAjax()
	{
		$this->_view['title'] = 'ajax检索供应商';
		$q = $_REQUEST['q'];
		$provider = Product_Provider::find('name like ?', $q.'%', '%'.$q.'%')->asArray()->getAll();
		$result = array();
		foreach ($provider as $p)
		{
			$result[] = array(
					0 => $p['name'],
					'id' => $p['id']
			);
		}
		echo json_encode($result);
		exit;
	}
	
	function actionProductInfoAjax()
	{
		$this->_view['title'] = 'ajax检索产品';
		$q = $_REQUEST['q'];
		$info = Product_basic::find('(product_basic.number like ? or product_basic.name like ?)', $q.'%', '%'.$q.'%')->asArray()->getAll();
		$ret = array();
		foreach ($info as $i)
		{
			$ret[] = array($i['number'] . ' ' . $i['name'], $i['id']);
		}
		echo json_encode($ret);
		exit;
	}
	
	function actionExportChecked()
	{
		$this->_view['title'] = '按勾选导出';
		if ($this->_context->isPOST())
		{
			$ids = json_decode($_POST['ids']);
			$search_result = Product_Basic::search(array('ids' => $ids), 0, 0, 'product_basic.number asc');
			if (count($search_result['data']) <= 1000)
			{
				Helper_BSS_XLS::ProductBasicExport($search_result['data']);
			}
			else
			{
				return $this->_redirectMessage('导出失败', '导出记录超过一千条..', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionExportSearch()
	{
		$this->_view['title'] = '按搜索条件导出';
		if ($this->_context->isPOST())
		{
			$url_arr = json_decode($_POST['url_arr'], 1);
			$search_result = Product_Basic::search($url_arr, 0, 0, 'product_basic.number asc');
			if (count($search_result['data']) <= 1000)
			{
				Helper_BSS_XLS::ProductBasicExport($search_result['data']);
			}
			else
			{
				return $this->_redirectMessage('导出失败', '导出记录超过一千条..', $_SERVER['HTTP_REFERER'], FAILUER);
			}
		}
	}
	
	function actionImport()
	{
		$this->_view['title'] = '导入（有限制）';
		$upload_result = Upload_Files::uploadFile('product_file', Q::ini('custom_flag/upload_file_type/product_basic/value'));
		if ($upload_result['ack'] == SUCCESS)
		{
			$product_basic = Helper_BSS_XLS::xlsFileToArray($upload_result['path'], $upload_result['file_id']);
			$error_message = '';
			foreach ($product_basic as $p)
			{
				$result = Product_Basic::import($p);
				if ($result['ack'] == FAILURE)
				{
					$error_message .= "\n" . $p['number'] . '：' . $result['message'];
				}
			}
			if ($error_message)
			{
				return $this->_redirectMessage('导入部分失败', $error_message, $_SERVER['HTTP_REFERER'], FAILURE);
			}
			else
			{
				return $this->_redirectMessage('导入成功', '正在返回..', $_SERVER['HTTP_REFERER'], SUCCESS);
			}
		}
		else
		{
			return $this->_redirectMessage('导入失败', '没有文件，正在返回..', $_SERVER['HTTP_REFERER'], $upload_result['ack']);
		}
	}
	
	function actionMoveCategorieschecked()
	{
		$this->_view['title'] = '按勾选移动分类';
		$ids = json_decode($_POST['ids']);
		$categories_id = json_decode($_POST['form_move_cat_id']);
		if (! empty($categories_id))
		{
			$has_child = Product_Categories::find('parent_id=?', $categories_id)->asArray()->getAll();
			if ($has_child)
			{
				return $this->_redirectMessage('批量失败', '该分类下还有子类，请选择..', $_SERVER['HTTP_REFERER'], FAILURE);
			}
			QDB::getConn()->execute('update product_basic set product_categories_id=' . $categories_id . ' where id in (' . implode(',', $ids) . ')');
			return $this->_redirectMessage('移动分类成功', '正在返回', $_SERVER['HTTP_REFERER'], SUCCESS);
		}
		else
		{
			return $this->_redirectMessage('移动分类失败', '移动分类选择不能为空', $_SERVER['HTTP_REFERER'], FAILURE);
		}
	}
	
	function actionMoveCategoriesSearch()
	{
		$this->_view['title'] = '按搜索移动分类';
		$condition = json_decode($_POST['url_arr'], 1);
		$categories_id = json_decode($_POST['form_move_cat_id']);
		if (! empty($categories_id))
		{
			$has_child = Product_Categories::find('parent_id=?', $categories_id)->asArray()->getAll();
			if ($has_child)
			{
				return $this->_redirectMessage('批量失败', '该分类下还有子类，请选择..', $_SERVER['HTTP_REFERER'], FAILURE);
			}
			$result = Product_Basic::search($condition, 0, 0);
			$ids = array();
			foreach ($result['data'] as $r)
			{
				$ids[] = $r['id'];
			}
			QDB::getConn()->execute('update product_basic set product_categories_id=' . $categories_id . ' where id in (' . implode(',', $ids) . ')');
			return $this->_redirectMessage('移动分类成功', '正在返回', $_SERVER['HTTP_REFERER'], SUCCESS);
		}
		else
		{
			return $this->_redirectMessage('移动分类失败', '移动分类选择不能为空', $_SERVER['HTTP_REFERER'], FAILURE);
		}
	}
	
	function actionPrint()
	{
		$this->_view['title'] = '打印产品基本资料（按勾选）'; 
		$ids = json_decode($_POST['ids']);
		$search_result = Product_Basic::search(array('ids' => $ids), 0, 0);
		Helper_BSS_Normal::getProductImgUrl($search_result['data'], 'number');
		$this->_view['product'] = $search_result['data'];
	}
}